import {GameRoom} from './room/GameRoom';
import {RedisPresence, Server} from 'colyseus';
import express from 'express';
import {createServer} from "http";

const port = Number(process.env.port) || 2567;

const app = express();
app.use(express.json());

// 初始化服务器
const gameServer = new Server({
    server: createServer(app),
    // ws连接前的校验
    verifyClient: function (info, next) {
        next(true);
    },
    // 当通过多个进程/机器扩展Colyseus时，需要提供一个状态服务器
    // presence: new RedisPresence(),
});

// Define a new room handler.
// @ts-ignore
gameServer.define("game", GameRoom, {})
    .on("create", (room) => console.log("room created:", room.roomId))
    .on("dispose", (room) => console.log("room disposed:", room.roomId))
    .on("join", (room, client) => console.log(client.id, "joined", room.roomId))
    .on("leave", (room, client) => console.log(client.id, "left", room.roomId));

// Listen on port
gameServer.listen(port).then(r => console.log('server is on port ' ,port));

// Make sure to never call the `simulateLatency()` method in production.
if (process.env.NODE_ENV !== "production") {
    // simulate 200ms latency between server and client.
    gameServer.simulateLatency(200);
}